
#/**********************************************************************
#@Author: Sangeeta
# This program wil be used to make revid <==> cvdid (bug fixing cvs ids)
# database
# **********************************************************************/

import MySQLdb
import re

#"""
user = "root"
password="123"
database = "apsec2014"
port=3306
"""
user = "sangeetal"
password="sangeetal"
database = "apsec2014"
port=3307
#"""

tables = ["chrome_browser"]

db1= MySQLdb.connect(host="localhost", user = user, passwd = password,db=database, port = port)
select_cursor = db1.cursor()
insert_cursor = db1.cursor()

#Extract revision ids having bug fixing 
bug_num1 = "bug[#= \t]*[0-9]+"
bug_num2 = "pr[#= \t]*[0-9]+"
bug_num3= "show[_]bug[.]cgi[?]id=[0-9]+"
bug_num4  = "fixe?[ds]?[= \t]*[0-9]+|bugs?[= \t]*[0-9]+|defects?[= \t]*[0-9]+|\
patch[= \t]*[0-9]+|issue[s]?[= \t]*[0-9]+"


#bug_num5 = "\[[0-9]+\]"
plain_num = "[0-9]+"


#word = "[A-Za-z0-9]+"
#str = "hello bug#89 hkjkj bug show_bug cgi?id=899 bug 67"
#p = re.compile(key_word)
#a=p.findall(str)
#print "match = ",a

def clean(log_message):
    log_message = log_message.replace(",", "  ") 
    log_message = log_message.lower()
    return log_message
    
for tab in tables:
    select_issue_str = "select issue_id from  `issue_"+tab+ "`  where regression=1 and \
     state='closed' and status in ('fixed', 'verified')"
    select_cursor.execute(select_issue_str)
    reg_ids_db = select_cursor.fetchall()
    reg_ids = list()
    for id in reg_ids_db:
        reg_ids.append(id[0])
    
    
    print "reg id=", reg_ids 
    select_str = "select id, logmessage   from revisions_"+tab 
    select_cursor.execute(select_str)    
    all_commits = select_cursor.fetchall()
    
    #Re_match = list()
    #Re_score = list()
    
    for d in all_commits:
        commit_id = d[0]
        log_message = d[1]
        log_message = clean(log_message)
        
        #print "commi id=", commit_id, " log", log_message  
        ids_in_log=list()
        
        p1 = re.compile(bug_num1)
        match_pat=p1.findall(log_message)        
        #print "match pat", match_pat
        for pattern in match_pat:
            id_pat = re.compile(plain_num)
            numeric_ids = id_pat.findall(pattern)
            for id in numeric_ids:
                ids_in_log.append(int(id))
         
        
        p2 = re.compile(bug_num2)
        match_pat=p2.findall(log_message)        
        #print "match pat", match_pat
        for pattern in match_pat:
            id_pat = re.compile(plain_num)
            numeric_ids = id_pat.findall(pattern)
            for id in numeric_ids:
                ids_in_log.append(int(id))        
            
            
        p3 = re.compile(bug_num3)
        match_pat=p3.findall(log_message)        
        #print "match pat", match_pat
        for pattern in match_pat:
            id_pat = re.compile(plain_num)
            numeric_ids = id_pat.findall(pattern)
            for id in numeric_ids:
                ids_in_log.append(int(id)) 
                
                
        p4 = re.compile(bug_num4)
        match_pat=p4.findall(log_message)
        #print "match pat", match_pat
        for pattern in match_pat:
            id_pat = re.compile(plain_num)
            numeric_ids = id_pat.findall(pattern)
            for id in numeric_ids:
                ids_in_log.append(int(id))        
       
       # Giving false postitives                
        #p5 = re.compile(bug_num5)
        #match_pat=p5.findall(log_message)        
        #print "match pat", match_pat
        #for pattern in match_pat:
        #    id_pat = re.compile(plain_num)
        #    numeric_ids = id_pat.findall(pattern)
        #    for id in numeric_ids:
        #        ids_in_log.append(int(id))          

        #print "ids in log", ids_in_log 
        #print "id1", id1
        #reg_ids.append(6)
        #print "reg ids", reg_ids
        #check if bugids are available in cvs log messages, insert them in table
        # value  = bug id, commitid =cvs id
        
        for bug_id_found_in_log in ids_in_log:
            print "bug id=", bug_id_found_in_log
            try:
                index =  reg_ids.index(bug_id_found_in_log) 
                insert_str = "insert into bugid_fixing_cvsid_"+tab+" values("+ (str)(bug_id_found_in_log)+",  "+\
                (str)(commit_id)+")"
                insert_cursor.execute(insert_str)
                print "Found1"
                db1.commit()
            except ValueError, e:
                print "Error",e
            except MySQLdb.IntegrityError, e:
                print e
             #   print "not found"

   
    

